﻿using System;
using System.IO;

namespace Helper.File
{
    /// <summary>
    /// 目录
    /// </summary>
    public class DirectoryHelper
    {
        /// <summary>
        /// 检测指定目录是否存在
        /// </summary>
        /// <param name="directoryPath">目录的绝对路径</param>
        /// <returns></returns>
        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="dir">要创建的目录路径包括目录名</param>
        public static void CreateDir(string dir)
        {
            if (dir.Length == 0) return;
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
        }
        /// <summary>
        /// 创建一个目录
        /// </summary>
        /// <param name="directoryPath">目录的绝对路径</param>
        public static void CreateDirectory(string directoryPath)
        {
            //如果目录不存在则创建该目录
            if (!IsExistDirectory(directoryPath))
            {
                CreateDir(directoryPath);
            }
        }

        /// <summary>
        /// 检测指定目录是否为空
        /// </summary>
        /// <param name="directoryPath">指定目录的绝对路径</param>        
        public static bool IsEmptyDirectory(string directoryPath)
        {
            try
            {
                //判断是否存在文件
                string[] fileNames = GetFileNames(directoryPath);
                if (fileNames.Length > 0)
                {
                    return false;
                }

                //判断是否存在目录
                string[] directoryNames = GetDirectories(directoryPath);
                if (directoryNames.Length > 0)
                {
                    return false;
                }

                return true;
            }
            catch
            {
                //这里记录日志
                //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
                return true;
            }
        }
        /// <summary>
        /// 获取指定目录中所有子目录列表,若要搜索嵌套的子目录列表,请使用重载方法.
        /// </summary>
        /// <param name="directoryPath">指定目录的绝对路径</param>        
        public static string[] GetDirectories(string directoryPath)
        {
            return Directory.GetDirectories(directoryPath);
        }

        /// <summary>
        /// 获取指定目录中所有文件列表
        /// </summary>
        /// <param name="directoryPath">指定目录的绝对路径</param>        
        public static string[] GetFileNames(string directoryPath)
        {
            //如果目录不存在，则抛出异常
            if (!IsExistDirectory(directoryPath))
            {
                throw new FileNotFoundException();
            }

            //获取文件列表
            return Directory.GetFiles(directoryPath);
        }



        /// <summary>
        /// 复制目录(递归)
        /// </summary>
        /// <param name="varFromDirectory">源目录路径</param>
        /// <param name="varToDirectory">目标目录路径</param>
        public static void CopyDirectory(string varFromDirectory, string varToDirectory)
        {
            Directory.CreateDirectory(varToDirectory);

            if (!Directory.Exists(varFromDirectory)) return;

            string[] directories = Directory.GetDirectories(varFromDirectory);

            if (directories.Length > 0)
            {
                foreach (string d in directories)
                {
                    CopyDirectory(d, varToDirectory + d.Substring(d.LastIndexOf("\\", StringComparison.Ordinal)));
                }
            }
            string[] files = Directory.GetFiles(varFromDirectory);
            if (files.Length > 0)
            {
                foreach (string s in files)
                {
                    System.IO.File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\\", StringComparison.Ordinal)), true);
                }
            }
        }

        /// <summary>
        /// 删除指定目录对应其他目录里的文件
        /// </summary>
        /// <param name="directoryPath">指定目录路径</param> 
        public static void DeleteDirectory(string directoryPath)

        {
            //如果目录不存在，则抛出异常
            if (!IsExistDirectory(directoryPath))
            {
                //    throw new FileNotFoundException();
            }
            DeleteDirectoryFiles(directoryPath);

            //获取文件列表
            Directory.Delete(directoryPath);

        }

        /// <summary>
        /// 删除指定目录对应其他目录里的文件
        /// </summary>
        /// <param name="directoryPath">指定目录路径</param> 
        public static void DeleteDirectoryFiles(string directoryPath)
        {
            Directory.CreateDirectory(directoryPath);
            string[] files = Directory.GetFiles(directoryPath);

            if (files.Length > 0)
            {
                foreach (string s in files)
                {
                    System.IO.File.Delete(directoryPath + s.Substring(s.LastIndexOf("\\", StringComparison.Ordinal)));
                }
            }
        }

    }
}
